package com.video.web.controller.app;

import com.alibaba.fastjson2.JSONObject;
import com.video.common.constant.CacheConstants;
import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.core.domain.entity.SysUser;
import com.video.common.core.domain.model.LoginUser;
import com.video.common.core.page.TableDataInfo;
import com.video.common.core.redis.RedisCache;
import com.video.common.exception.ServiceException;
import com.video.common.utils.StringUtils;
import com.video.framework.web.service.TokenService;
import com.video.video.domain.DUser;
import com.video.video.domain.RegisterSettings;
import com.video.video.domain.app.dto.AppUserRegisterDto;
import com.video.video.domain.app.dto.LoginDto;
import com.video.video.service.IDUserService;
import com.video.video.service.IRegisterSettingsService;
import com.video.web.service.AppDUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "App登录/注册")
@RestController
@RequestMapping("/app")
public class LoginController extends BaseController {
  @Resource
  private TokenService tokenService;
  @Resource
  private AppDUserService appDUserService;
  @Autowired
  private RedisCache redisCache;
  @Autowired
  private IRegisterSettingsService registerSettingsService;

  @ApiOperation(value = "注册设置")
  @GetMapping("/getRegisterInfo")
  public AjaxResult getRegisterInfo()
  {
    List<RegisterSettings> list = registerSettingsService.selectRegisterSettingsList(new RegisterSettings());
    RegisterSettings registerSettings=new RegisterSettings();
    if(CollectionUtils.isNotEmpty(list)){
      registerSettings=list.get(0);
      return AjaxResult.success(registerSettings);
    }else {
      return AjaxResult.error("系统未设置注册信息");
    }

  }

  @ApiOperation(value = "注册")
  @PostMapping("/register")
  public AjaxResult register(@Validated AppUserRegisterDto user) {
    logger.info("注册用户:" + JSONObject.toJSONString(user));

    DUser dUser =  appDUserService.register(user);
    LoginUser loginUser = new LoginUser();
    SysUser sysUser = new SysUser();
    loginUser.setUser(sysUser);
    loginUser.setUserId(Long.getLong(dUser.getId()));
    String token = tokenService.createToken(loginUser);
    return AjaxResult.success("注册成功", token);
  }

  @ApiOperation(value = "登录")
  @PostMapping("/login")
  public AjaxResult login(@Validated LoginDto loginDto) {
    logger.info("登录:" + JSONObject.toJSONString(loginDto));
    if (StringUtils.isEmpty(loginDto.getUsername())) {
      throw new ServiceException("用户名/手机/邮箱不能为空！", 10070);
    }
    if (loginDto.getUsername().length() < 1) {
      throw new ServiceException("至少1位", 10071);
    }
    if (StringUtils.isEmpty(loginDto.getPassword())) {
      throw new ServiceException("密码不能为空", 10072);
    }
    if (loginDto.getPassword().length() > 10 || loginDto.getPassword().length() < 6) {
      throw new ServiceException("密码至少6位,最大10位", 10082);
    }
    DUser login = appDUserService.login(loginDto);
    if(login.getStatus().equals("2")){
      throw new ServiceException("账号已被禁用", 10082);
    }


    LoginUser loginUser = new LoginUser();
    SysUser sysUser = new SysUser();
    loginUser.setUser(sysUser);
    loginUser.setUserId(Long.valueOf(login.getId()));
    String token = tokenService.createToken(loginUser);
    Map<String,String> result = new HashMap<>();
    result.put("token",token);
    return AjaxResult.success("登录成功", result);
  }

  @ApiOperation(value = "退出登录")
  @DeleteMapping("appLogout")
  public AjaxResult appLogout(String tokenId) {
    redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
    return AjaxResult.success();
  }

}
